package com.xiangdao.controller;

import com.xiangdao.dao.MessageDao;
import com.xiangdao.dao.PostMainDao;
import com.xiangdao.dao.ShareProductDao;
import com.xiangdao.dao.UserDao;
import com.xiangdao.model.PostMain;
import com.xiangdao.model.ShareProduct;
import com.xiangdao.model.User;
import com.xiangdao.util.HostHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Map;

@Controller
public class FirstController {
    @Autowired
    ShareProductDao shareProductDao;
    @Autowired
    UserDao userDao;

    @Autowired
    PostMainDao postMainDao;

    @Autowired
    MessageDao messageDao;

    @Autowired
    HostHolder hostHolder;

    @RequestMapping(path = "/home_page/{i}", method = RequestMethod.GET)
    public String displayShareProduct(String id, Model model, @PathVariable int i) {
        int a = i;
        System.out.println(a);
//        User user = new User();
//        Integer user_id = user.getId();
//        PostMain postMain = postMainDao.findPostMainById(Integer.valueOf(id));
//        ShareProduct shareProduct = shareProductDao.findShareProductById(Integer.valueOf(id));
        ArrayList<ShareProduct> shareProducts = shareProductDao.findAllProduct();
        ArrayList<PostMain> postMains = postMainDao.findAllPostMain();
        while (postMains.size()>4){
            postMains.remove(postMains.size()-1);
        }
        Map<Integer,User> map = new HashMap();
        for (ShareProduct s :
                shareProducts) {
            map.put(s.getId(),userDao.getUserById(s.getUser_id()));
            Integer integer = messageDao.countMessageByShareProduct(s.getId());
            shareProductDao.updateProduct(integer, s.getId());
        }
        ArrayList<ShareProduct> shareProductss = shareProductDao.findAllProduct();
        for (int x=shareProductss.size();x>8;x--){
            shareProductss.remove(x-1);
        }
        if (i == 0||i==2) {
            shareProductss.sort(new Comparator<ShareProduct>() {
                @Override
                public int compare(ShareProduct o1, ShareProduct o2) {
                    return o1.getTitle().compareTo(o2.getTitle());
                }
            });
        }
        if(i==1||i==0){
            postMains.sort(new Comparator<PostMain>() {
                @Override
                public int compare(PostMain o1, PostMain o2) {
                    return o1.getTime().compareTo(o2.getTime());
                }
            });
        }
//        int messageNum = messageDao.countMessageByShareProduct(Integer.valueOf(id));
//        model.addAttribute("postMain", postMain);
//        model.addAttribute("user_id", user_id);
//        model.addAttribute("userDao", userDao);
//        model.addAttribute("shareProduct", shareProduct);
//        model.addAttribute("user", user);
        model.addAttribute("shareProducts", shareProductss);
        model.addAttribute("postMains", postMains);
        model.addAttribute("a", a);
        model.addAttribute("userMap", map);

        System.out.println("产品数量"+shareProducts.size());
        System.out.println("帖子数量"+postMains.size());
        System.out.println(postMains.get(0).getUser_id());
        System.out.println(a);
        System.out.println(map);
        return "home_page";
    }
    //123
}
